package com.dang.alex.servlet.shoppingCar;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;

import com.dang.alex.model.CartItem;
import com.dang.alex.service.imp.ProductServiceImp;
import com.dang.alex.service.imp.ServiceDAOFactory;

@WebServlet({ "/CarServlet", "/car.do" })
public class CarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static final String METHOD_ADD="0";
	public static final String METHOD_DELETE="1";
	public static final String METHOD_EDIT="2";
	public static final String METHOD_CLEAR="3";
	public static final String METHOD_LIST="4";
	public static final String METHOD="5";
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method=req.getParameter("method");
		HttpSession session=req.getSession();
		Map<Integer,CartItem> map=(Map<Integer,CartItem>) session.getAttribute("s_car");
		if(method!=null){
			switch(method){
			case CarServlet.METHOD_ADD:
				add(req,resp);
				break;
			case CarServlet.METHOD_LIST:
				list(req,resp,map);
				break;
			case CarServlet.METHOD_DELETE:
				map.remove(new Integer(req.getParameter("id")));
				list(req,resp,map);
				break;
			case CarServlet.METHOD_EDIT:
				String num=req.getParameter("num");
				Integer id=new Integer(req.getParameter("id"));
				CartItem cartItem=map.get(id);
				cartItem.setNum(new Integer(num).intValue());
				map.put(id, cartItem);
				list(req, resp, map);
				break;
			case CarServlet.METHOD_CLEAR:
				map.clear();
				list(req, resp, map);
				break;
			case CarServlet.METHOD:
				
				action(req,resp);
				break;
			default:
				break;
			}
		}
		
		
		
	}

	private void action(HttpServletRequest req, HttpServletResponse resp) {
		try {
			System.out.println(3);
			req.getRequestDispatcher("/WEB-INF/view/car/cart_list.jsp").forward(req, resp);
			System.out.println(4);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private void list(HttpServletRequest req, HttpServletResponse resp, Map<Integer, CartItem> map) {
		req.setAttribute("totalDang", total(map,false));
		req.setAttribute("totalSave", totalSava(total(map,true) - total(map,false)));
		req.setAttribute("s_car", map);
		try {
			req.getRequestDispatcher("/WEB-INF/view/car/cart_list.jsp").forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("static-access")
	private double totalSava(double totalSava) {
		BigDecimal bd=new BigDecimal(totalSava);
		BigDecimal bd1=bd.setScale(2, bd.ROUND_HALF_UP);
		
		return bd1.doubleValue();
	}

	@SuppressWarnings("static-access")
	private double total(Map<Integer, CartItem> map, boolean flag) {
		double totalPrice=0.0;
		if(flag){
			for(Map.Entry<Integer, CartItem> entry: map.entrySet()){
				totalPrice +=entry.getValue().getNum()*entry.getValue().getPro().getFixedPrice();
			}
		}
		else{
			for(Map.Entry<Integer, CartItem> entry: map.entrySet()){
				totalPrice +=entry.getValue().getNum()*entry.getValue().getPro().getDangPrice();
			}
		}
		BigDecimal bd=new BigDecimal(totalPrice);
		BigDecimal bd1=bd.setScale(2, bd.ROUND_HALF_UP);
		return bd1.doubleValue();
	}

	private void add(HttpServletRequest req, HttpServletResponse resp) {

		boolean flag=true;
		Map<Integer,CartItem> map=null;
		try {
			Integer id=new Integer(req.getParameter("id"));
			HttpSession session=req.getSession();
			
			//判断是否登录
			if(session.getAttribute("s_user")==null){
				flag=false;
			}else{
				map=(Map<Integer, CartItem>) session.getAttribute("s_car");
				if(map==null){
					map=new HashMap<Integer,CartItem>();
				}
				if(map.containsKey(id)){
					CartItem cartItem=map.get(id);
					cartItem.setNum(cartItem.getNum()+1);
				}else{
					CartItem cartItem=new CartItem();
					ProductServiceImp service=ServiceDAOFactory.getProductServiceImp();
					cartItem.setPro(service.findById(id));
					map.put(id, cartItem);
				}
			}
			session.setAttribute("s_car", map);
			JSONObject json=new JSONObject();
			json.put("flag", flag);
			PrintWriter out=resp.getWriter();
			out.print(json.toString());
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
